﻿var DrawingUtility = new function () {

    this.UniverseDrawer = new UniverseEntityDrawBase();

    this.OnUniverseDepthChanged = function (universeEntity) {
        DrawingUtility.RefreshHtml(universeEntity);
    };
    
    this.OnShootingStarOccurence = function (universeEntity) {
    	var starsCount = universeEntity.ShootingStarsCount.RandomInt();
    	var vectorRange = new Range(-300, 300);
    	for(var index = 0; index < starsCount; index++){
    		var shootingStar = new ShootingStar(new Rectangle(universeEntity.SizeAndPosition.Size.Random(), new Size(3, 3)), new Vector(vectorRange.Random(), vectorRange.Random()), '');
    		DrawingUtility.DrawToHtml(shootingStar, universeEntity.AssociatedJqObject);
    	}
    };

    this.RefreshHtml = function (universeEntity) {
        DrawingUtility.UniverseDrawer.Refresh(universeEntity);
        var children = universeEntity.Children();
        if (children.length > 0) {
            for (var index in children) {
                DrawingUtility.RefreshHtml(children[index]);
            }
        }
        ProcessOpacity(universeEntity);
    };

    this.DrawToHtml = function (universeEntity, canvasJq) {

        //Recurrence stop condition
        if (canvasJq == null || universeEntity == null)
            return;

        if (universeEntity.UniverseEntityType == UniverseEntityTypes.Universe) {
            universeEntity.AddViewDepthChangedHandler(DrawingUtility.OnUniverseDepthChanged);
            universeEntity.AddShootingStarsOccuranceHandler(DrawingUtility.OnShootingStarOccurence);
        }

        var universeEntityJq = UniverseEntityJqObject(universeEntity);
        canvasJq.append(universeEntityJq);

        //Animate
        if (universeEntity.UniverseEntityType == UniverseEntityTypes.ShootingStar)
        	this.UniverseDrawer.AnimationUtility.ShootingStarAnimation(universeEntity);
        if (universeEntity.UniverseEntityType == UniverseEntityTypes.StarField)
        	this.UniverseDrawer.AnimationUtility.StarFieldAnimation(universeEntity, this.UniverseDrawer.DrawStarField);
        else if (universeEntity.UniverseEntityType == UniverseEntityTypes.Planet)
        	this.UniverseDrawer.AnimationUtility.PlanetRotation(universeEntity);
        else if (universeEntity.UniverseEntityType == UniverseEntityTypes.SolarSystem)
        	this.UniverseDrawer.AnimationUtility.SolarSystemRotation(universeEntity);
        //Animate END

        var children = universeEntity.Children();

        if (children.length > 0) {
            for (var index in children) {
                DrawingUtility.DrawToHtml(children[index], universeEntityJq);
            }
        }

        ProcessOpacity(universeEntity);

    };

    //Private Helper Methods
    function UniverseEntityJqObject(universeEntity) {

        var jqObject = DrawingUtility.UniverseDrawer.Draw(universeEntity);

        universeEntity.AssociatedJqObject = jqObject;

        return jqObject;
    };

    function ProcessOpacity(universeEntity) {

        if (universeEntity.UniverseEntityType != UniverseEntityTypes.Galaxy)
            return;

        var opacity = 1 - Math.abs(universeEntity.GetDepth());

        if (opacity < 1)
            universeEntity.AssociatedJqObject.css({ opacity: opacity });
        else
            universeEntity.AssociatedJqObject.css({ opacity: '' });
    };

};